import request from "@/utils/request";

export const api = {
  // 微信授权登录
  wechatAuth: (data) => {
    return request.post("/wechat/auth", data);
  },

  // 获取微信JSAPI配置
  getWechatConfig: (params) => {
    return request.get("/wechat/config", {
      params,
    });
  },

  // 获取用户信息
  getUserInfo: () => {
    return request.get("/user/profile");
  },

  // 更新用户信息
  updateUserInfo: (data) => {
    return request.put("/user/profile", data);
  },

  // --------------------------
  // 获取微信授权url
  getWechatRedirectUrl: (params) => {
    return request.get("/wxAuth/getWechatRedirectUrl", { params });
  },

  // code换取token
  getToken: (data) => {
    return request.post("/wxAuth/getToken", data);
  },

  // 微信登陆shopid
  wxLogin: (data) => {
    return request.post("/wxUser/login", data);
  },

  // 微信登出
  wxLogout: (data) => {
    return request.post("/wxUser/logout", data);
  },

  getShopInfo: (params) => {
    return request.get(`/shopInfo/queryStoreBusinessInfo`, {
      params,
    });
  },
  getRecommendedProducts: (params) => {
    return request.get("/product/getRecommendedProducts", {
      params,
    });
  },

  // 添加商品到购物车
  addToCart: (data) => {
    return request.post("/ShoppingCart/addShoppingCart", data);
  },

  // 更新购物车商品数量
  updateCart: (data) => {
    return request.post("/ShoppingCart/updateShoppingCart", data);
  },

  // 获取购物车列表
  getCartList: (params) => {
    return request.get("/ShoppingCart/getShoppingCartList", {
      params,
    });
  },

  // 选中或取消购物车商品
  updateShoppingCartIsSelected: (data) => {
    return request.post("/ShoppingCart/updateShoppingCartIsSelected", data);
  },

  // 删除购物车商品
  deleteShoppingCart: (data) => {
    return request.post("/ShoppingCart/deleteShoppingCart", data);
  },

  // 创建订单
  createOrder: (data) => {
    return request({
      method: "POST",
      url: "/wxPay/createOrder",
      data: data,
      headers: {
        "Content-Type": "application/json",
      },
    });
  },

  getWechatConfig: (params) => {
    return request.get("/wechat/config", {
      params,
    });
  },

  // 获取主订单列表
  getMainOrder: (params) => {
    return request.get("/wxPay/getMainOrder", {
      params,
    });
  },

  // 获取子订单明细
  getOrderItem: (params) => {
    return request.get("/wxPay/getOrderItem", {
      params,
    });
  },

  // 获取签名信息
  getPaySign: (prepayId) => {
    return request({
      method: "POST",
      url: `/wxPay/getPaySign?prepayId=${prepayId}`,
    });
  },

  // 更新微信用户信息
  updateWxUserInfo: (data) => {
    return request.post("/wxUser/updateUserInfo", data, {
      headers: {
        "Content-Type": "application/json",
      },
    });
  },

  // 获取全部商品
  getAllProducts: (params) => {
    return request.get("/product/getAllProducts", {
      params,
    });
  },

  // 获取门店banner图片
  getShopPicset: (params) => {
    return request.get("/shopInfo/getShopPicset", {
      params,
    });
  },

  // 获取服务端token
  getBuildJsSdkSignature: (data) => {
    return request.post("/wxAuth/getBuildJsSdkSignature", data);
  },

  uploadImage: (file) => {
    const formData = new FormData();
    formData.append('file', file);
    return request.post("/upload/uploadImg", formData);
  },
};
